package sharetime.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringBeanUtils implements ApplicationContextAware {
	private static ApplicationContext applicationContext; // Spring Context

	private static Logger logger = LoggerFactory.getLogger(SpringBeanUtils.class);

	/*
	 * private static SpringContextUtil instance = new SpringContextUtil();
	 * 
	 * public static SpringContextUtil getInstance(){ return instance; }
	 */

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("entering setApplicationContext method...");
		}
		SpringBeanUtils.applicationContext = applicationContext;
	}

	/**
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static Object getBean(String name) throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("entering getBean method...");
		}
		return applicationContext.getBean(name);
	}

	public static Object getBean(String name, Class requiredType)
			throws BeansException {
		return applicationContext.getBean(name, requiredType);
	}

	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}

	public static boolean isSingleton(String name)
			throws NoSuchBeanDefinitionException {
		return applicationContext.isSingleton(name);
	}

	public static Class getType(String name)
			throws NoSuchBeanDefinitionException {
		return applicationContext.getType(name);
	}

	public static String[] getAliases(String name)
			throws NoSuchBeanDefinitionException {
		return applicationContext.getAliases(name);
	}
}
